import { Component, OnInit, ViewChild, ViewContainerRef, ComponentRef, ComponentFactoryResolver } from '@angular/core';
import { Router, NavigationEnd, ActivatedRoute } from '@angular/router';
import { AppPaginationComponent, PaginationModel } from "../../../widgets/pagination/pagination.component";
import { FileHistoryComponent } from "../modal/file-history/file-history.component";
import { FundInfoComponent } from "../modal/fund-info/fund-info.component"
import { ListService } from './list.service';
import { Util } from 'app/common/util';
import * as moment from 'moment';
import * as _ from 'lodash'

@Component({
  selector: 'app-stage',
  templateUrl: './list.component.html',
  styleUrls: ['./list.component.scss'],
  providers: [ListService]
})
export class ListComponent implements OnInit {
  // 历史附件模版
  @ViewChild("fileHistory", { read: ViewContainerRef })
  fileHistory: ViewContainerRef;
  fileHistoryComponent: ComponentRef<FileHistoryComponent>;
  //查看基金信息模板
  @ViewChild("fundInfo", { read: ViewContainerRef })
  fundInfo: ViewContainerRef;
  fundInfoComponent: ComponentRef<FundInfoComponent>;
  // 产品文件树sFundType
  public sFundType: any = '';
  // 产品文件树基金类别
  public fundType: any = [];
  // 产品文件树附件大类型
  public attachType1: any = [];
  // 产品文件树附件小类型
  public attachType2: any = [];
  // 产品文件树中类
  // 产品文件树小类
  // 列表是否全选
  public isCheckedAll = false;
  // 产品文档列表
  public dataList = [];
  // 查询分页列表post数据
  public getListPost: any = {
    pageIndex: 1,
    pageSize: 10,
    attachName: '',
    fundName: '',
    fundType: '',
    attachType1: '',
    attachType2: '',
  };
  //产品文档分页配置
  public listPageInfo: PaginationModel = {
    currentPageNum: 1,
    pageSize: 10,
    totalPages: 1,
    total: 0,
    pagesShow: 5,
    startRow: 0,
    endRow: 0,
    pageList: [5, 10, 25, 50, 100]
  };

  constructor(
    public resolver: ComponentFactoryResolver,
    public router: Router,
    private route: ActivatedRoute,
    public listService: ListService,
  ) {

  }

  ngOnInit() {
    this.list();
    this.getAttachTypeTree();
  }

  // 列表全选
  checkedAll() {
    if (this.isCheckedAll) { // 更新为全选
      _.forEach(this.dataList, item => {
        item.isChecked = true;
      });
    } else { // 更新为不全选
      _.forEach(this.dataList, item => {
        item.isChecked = false;
      });
    }
  }
  // 多个单选后列表是否全选
  ctrlCheckAll() {
    let that = this;
    let checkList = [];
    _.forEach(this.dataList, item => {
      if (item.isChecked == true) {
        checkList.push(item);
      }
    })
    if (checkList.length == this.dataList.length) {
      this.isCheckedAll = true;
    } else {
      this.isCheckedAll = false;
    }
  }
  // 查询分页列表
  list(pageNum?, pageSize?) {
    let that = this;
    this.isCheckedAll = false;
    this.getListPost.attachName = _.trim(this.getListPost.attachName);
    this.getListPost.fundName = _.trim(this.getListPost.fundName);
    this.getListPost.pageIndex = pageNum ? pageNum : this.listPageInfo.currentPageNum;
    this.getListPost.pageSize = pageSize ? pageSize : this.listPageInfo.pageSize;
    this.listService.list(this.getListPost).subscribe(data => {
      if (data) {
        that.dataList = data.list;
        that.listPageInfo.currentPageNum = data.pageNum;
        that.listPageInfo.totalPages = data.pages;
        that.listPageInfo.total = data.total;
        that.listPageInfo.startRow = data.startRow;
        that.listPageInfo.endRow = data.endRow;
      }
    });
  }
  // 按选取个数分页
  listpageSizeChange(pageSize: number) {
    if (pageSize !== this.listPageInfo.pageSize) {
      this.listPageInfo.pageSize = pageSize;
      this.list();
    }
  }
  // 页码分页导航
  listPageNavigation(currentPageNum: number) {
    this.listPageInfo.currentPageNum = currentPageNum;
    this.list(this.listPageInfo.currentPageNum, this.listPageInfo.pageSize);
  }
  // 获取产品文件树
  getAttachTypeTree() {
    let that = this;
    this.listService.getAttachTypeTree(this.sFundType).subscribe(data => {
      that.fundType = data;
    })
  }
  // 获取产品文件数附件大分类
  getAttachType1() {
    let that = this;
    if (this.getListPost.fundType == '') {
      this.attachType1 = [];
      this.attachType2 = [];
      this.getListPost.attachType1 = '';
      this.getListPost.attachType2 = '';
    } else {
      _.forEach(this.fundType, item => {
        if (item.sFundTypeCode == that.getListPost.fundType) {
          that.attachType1 = item.childrenAttachType;
          that.getListPost.attachType1 = '';
          that.getListPost.attachType2 = '';
        }
      })
    }
  }
  // 获取产品文件数附件小分类
  getAttachType2() {
    let that = this;
    if (this.getListPost.attachType1 == '') {
      this.attachType2 = [];
      this.getListPost.attachType2 = '';
    } else {
      _.forEach(this.attachType1, item => {
        if (item.sAttachTypeCode == this.getListPost.attachType1) {
          that.attachType2 = item.childrenAttachType;
          that.getListPost.attachType2 = '';
        }
      })
    }
  }

  // 批量下载附件
  attachDownload() {
    // 获取下载附件的内码
    let checkedItem = _.concat([], _.map(_.filter(this.dataList, { isChecked: true }), "nAttachNo"));
    if (checkedItem.length == 0) {
      window['swal']("提示", "请选择附件再下载 !", "warning")
    } else {
      // 下载附件
      Util.attachDownload(checkedItem);
    }
  }
  // 下载附件
  downLoadFile(item) {
    if (item.nAttachNo) {
      Util.attachDownload([item.nAttachNo]);
    } else {
      window['swal']("提示", "暂无文件下载", "warning");
    }
  }
  // 历史附件模版
  showFileHistory(item) {
    // 判断历史附件模版已存在，先注销
    if (this.fileHistoryComponent && this.fileHistoryComponent.instance) {
      this.fileHistoryComponent.destroy();
    }
    // 动态加在历史附件模版
    const fileHistoryComponent = this.resolver.resolveComponentFactory(FileHistoryComponent);
    this.fileHistoryComponent = this.fileHistory.createComponent(fileHistoryComponent);
    // 组件赋值 产品基础信息
    this.fileHistoryComponent.instance.fileHistoryVersions = item;
    // 打开历史附件模版
    window['$']("#fileHistory-modal").modal();
  }

  //查看基金信息模板
  showFundInfo(item) {
    // 判断基金信息模版已存在，先注销
    if (this.fundInfoComponent && this.fundInfoComponent.instance) {
      this.fundInfoComponent.destroy();
    }
    // 动态加在基金信息模版
    const fundInfoComponent = this.resolver.resolveComponentFactory(FundInfoComponent);
    this.fundInfoComponent = this.fundInfo.createComponent(fundInfoComponent);
    // 组件赋值 产品基础信息
    this.fundInfoComponent.instance.listFundInfo = item;
    // 打开基金信息模版
    window['$']("#fundInfo-modal").modal();
  }
  // 点击表格头排序
  orderData(colName) {
    let listDataAsc = _.orderBy(this.dataList, [colName], ["asc"]);
    if (JSON.stringify(this.dataList) == JSON.stringify(listDataAsc)) {
      listDataAsc = _.orderBy(this.dataList, [colName], ["desc"]);
    }
    this.dataList = listDataAsc;
  }
}

